package org.smile.homework01.paladin.parking;

import java.util.Scanner;

public class Driver {

    public Cars buyCar(){
        Cars car = new Cars();
        return car;
    }

    public void inbound(ParkingLot p, Cars car){
        ParkingUnits[][] units = p.getUnits();
        System.out.println("入库车辆尺寸为：" + car.getSize());
        out:
        for(int i = 0; i <units.length; i++){
            for(int j = 0; j < units[0].length; j++){
                if(units[i][j].getStatus() == 0){
                    System.out.println("找到空闲车位开始判断车位大小");
                    if(units[i][j].getSize() == car.getSize()){
                        System.out.println("车位大小合适，可以停放");
                        System.out.println(car.getNamePlate() + " 停放于 " + units[i][j].getName());
                        units[i][j].setCar(car);
                        units[i][j].setNamePlate(car.getNamePlate());
                        units[i][j].setStatus(1);
                        break out;
                    }else{
                        System.out.println("车位尺寸不合适，寻找下一车位");
                    }
                }else{
                    System.out.println("车位占用，寻找下一车位");
                }
            }
        }
    }

    public void outBounds(ParkingLot p){
        System.out.println("请输入车牌号");
        Scanner in = new Scanner(System.in);
        String namePlate = in.nextLine();

        ParkingUnits parkingUnit = findCar(namePlate,p);

        if(parkingUnit != null){
            int usedTime = (int)(Math.random()*10);
            double amountNow = usedTime*parkingUnit.getPrice();
            double amount = parkingUnit.getAmount();
            amount = amount + amountNow;
            parkingUnit.setAmount(amount);

            System.out.println("本次停车" + usedTime + "小时，收费：" + amountNow + "元");
            parkingUnit.setAmountNow(usedTime*parkingUnit.getPrice());
            parkingUnit.setStatus(0);
            parkingUnit.setNamePlate("");
        }

    }

    public ParkingUnits findCar(String n,ParkingLot p){
        ParkingUnits[][] units = p.getUnits();
        ParkingUnits parkingUnit = null;
        out:
        for(int i = 0; i <units.length; i++){
            for(int j = 0; j < units[0].length; j++){
                String namePlate = units[i][j].getNamePlate();
                if(namePlate.equals(n)){
                    System.out.println("找到车辆" + n + " 停放于 " + units[i][j].getName());
                    parkingUnit = units[i][j];
                    break out;
                }else{
                    System.out.println(units[i][j].getName() + "未找到车辆");
                    parkingUnit = null;
                }
            }
        }
        return parkingUnit;
    }

    public void findingParkingLot(int i){

    }

    public static void main(String[] args){
        ParkingLot p = new ParkingLot(2,2);
        Driver driver = new Driver();
        p.checkCompound(p);
        while(true){
            Scanner in = new Scanner(System.in);
            System.out.println("请输入你需要的操作 1：入库，2：出库，3：结费，4：查看在场车辆，5：结束操作");
            int operation = in.nextInt();

            if(operation == 1){
                Cars car = driver.buyCar();
                driver.inbound(p, car);
            }

            if(operation == 2){
                driver.outBounds(p);
            }

            if(operation == 3){
                p.getTotalAmount();
            }

            if(operation == 4){
                p.checkCompound(p);
            }

            if(operation == 5){
                break;
            }
        }
    }
}
